﻿$(function() {
	var branchList = $('#branchList').datagrid({
	    title:'设备网点信息', 
	    fitColumns:true,
	    url:'branch!getBranchPage?returnType=json',
	    idField:'branchId', 
	    singleSelect:false,//是否单选 
	    pagination:true,//分页控件 
	    rownumbers:true,//行号 
	    toolbar: [{
	        text: '添加', 
	        iconCls: 'icon-add', 
	        handler: function() { // 回调函数
	            var branchAddDialog = $('#branch_addDialog').dialog({
	                width: 750,
	                height: 350,
	                href: 'jsp/branch/branchAdd.jsp',
	                modal: true,
	                shadow: false,
	                title: '添加信息',
	                onLoad:function(){
	                	initProvCity("#branch_addForm","福建","福州");
	                },
	                buttons: [{
	                    text: '确定',
	                    iconCls: 'icon-add',
	                    handler: function() {
                    		$('#branch_addForm').form('submit', {
                    			url: 'branch!addBranch',
                    			success: function(json) {
                    				var result = jQuery.parseJSON(json);
                    				if (result.success) {
                    					$("#branchList").datagrid('reload');
                    					$("#branchList").datagrid('unselectAll');
                    					branchAddDialog.dialog('close');
                    				}
                    				showMsg(result.msg);
                    			}
                    		});
	                    }
	                },
	                {
	                    text: '取消',
	                    iconCls: 'icon-remove',
	                    handler: function() {
	                        $('#branch_addDialog').dialog('close');
	                    }
	                }]
	            });
	            $('#branch_addDialog').window('center');
	        } 
	    }, '-', { 
	        text: '修改', 
	        iconCls: 'icon-edit', 
	        handler: function() {
                var rows = $('#branchList').datagrid('getSelections');
                if (rows.length == 1) {
                    var branchUpdateDialog = $('#branch_updateDialog').dialog({
                        width: 800,
                        height: 350,
                        href: 'jsp/branch/branchEdit.jsp',
                        modal: true,
                        shadow: false,
                        title: '修改信息',
                        buttons: [{
                            text: '确定',
                            iconCls: 'icon-ok',
                            handler: function() {
                        		$('#branch_updateForm').form('submit', {
                        			url: 'saveOrUpateIpaddress.do?type=1',
                        			success: function(json) {
                        				var result = jQuery.parseJSON(json);
                        				if (result.success) {
                        					$('#branchList').datagrid('reload');
                        					$("#branchList").datagrid('unselectAll');
                        					branchUpdateDialog.dialog('close');
                        				}
                        				showMsg(result.msg);
                        			}
                        		});
                            }
                        },
                        {
                            text: '取消',
                            iconCls: 'icon-remove',
                            handler: function() {
                                $('#branch_updateDialog').dialog('close');
                            }
                        }],
                        onLoad: function() {
                            $('#branch_updateForm').form('load', rows[0]);
                            initProvCity("#branch_updateForm",rows[0].branchProvince,rows[0].branchCity);
                            //console.log($('#branch_updateForm').find('#smsTemplate'));
                            //console.log(rows[0].agent.userSmsTemplate);
                            $('#branch_updateForm').find('#smsTemplate').textbox('setValue',rows[0].agent.userSmsTemplate);
                        }
                    });
                    $('#branch_updateDialog').window('center');
                } else {
                    $.messager.alert('提示', '请选择一条记录进行编辑！', 'info');
                }
            } 
	    }, '-',{
	        text: '删除', 
	        iconCls: 'icon-remove', 
	        handler: function(){
                var rows = branchList.datagrid('getSelections');
                if (rows.length <= 0) {
                    $.messager.alert('提示', '请选择要删除的记录!', 'info');
                } else {
                    var ids = "";
                    for (var i = 0; i < rows.length; i++) {
                    	if(i==(rows.length-1)){
                    		ids = ids + rows[i].branchId;
                    	}else{
                    		ids = ids + rows[i].branchId + ',';
                    	}
                    }
                    $.messager.confirm('提示', '您确定要删除选择的记录吗?',
                    function(t) {
                        if (t) {
                            $.ajax({
                                type: 'post',
                                url: 'branch!deleteBranch?ids=' + ids,
                                success: function(result) {
                                    if (result.success) {
                                        branchList.datagrid('acceptChanges');
                                        editRow = undefined;
                                        branchList.datagrid('reload');
                                    }
                                    branchList.datagrid('unselectAll');
                                    showMsg(result.msg);
                                }
                            });

                        }
                    });
                }
            }
	    }], 
	});
	//右下角提示
	function showMsg(message){
		$.messager.show({
			title: '提示',
			msg: message
		});
	}
	//初始化省市信息
	function initProvCity(dom,p,c){
		$(dom).citySelect({
			nodata:"none",
			required:true,
			prov:p,
			city:c
		}); 
	}
});